import { createSlice } from "@reduxjs/toolkit";
import axios from "axios";

const BillStore = createSlice({
  name: "bill",
  initialState: {
    list: [],
  },
  reducers: {
    setList(state, { payload }) {
      state.list = payload;
    },
    addList(state, { payload }) {
      state.list.push(payload);
    },
  },
});

const BillReducer = BillStore.reducer;

const { setList, addList } = BillStore.actions;

const getList = () => {
  return async (dispatch) => {
    const res = await axios.get("http://localhost:8888/ka");
    dispatch(setList(res.data));
  };
};

const addBillList = (data) => {
  return async (dispatch) => {
    const res = await axios.post("http://localhost:8888/ka",  data );
    dispatch(addList(res.data));
  };
};
export { getList, addBillList };
export default BillReducer;
